﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Collections;

namespace Bowerbird.Core.Storage
{
    public static class UserRepository
    {

        public static User GetByNameIdentifier(string identityProvider, string nameIdentifier)
        {
            User obj = default(User);
            IList exp = new ArrayList();

            exp.Add(NHibernate.Criterion.Expression.And(
                NHibernate.Criterion.Expression.Eq("IdentityProvider", identityProvider),
                NHibernate.Criterion.Expression.Eq("NameIdentifier", nameIdentifier)));

            IList<User> list = NHibernateManager.GetObjectList<User>(exp);

            if (list.Count == 1)
                obj = list[0];
            else if (list.Count > 1)
                throw new Exception("Unexpected entity count exception. Only one entity was expected.");

            return obj;
        }

        public static User Register(string identityProvider, string nameIdentifier, string name = "")
        {
            User user = GetByNameIdentifier(identityProvider, nameIdentifier);

            if (user == null)
            {
                user = new User
                {
                    IdentityProvider = identityProvider,
                    NameIdentifier = nameIdentifier,
                    Name = name
                };

                user = RepositoryBase<User>.SaveOrUpdate(user);
            }

            return user;
        }

        public static IList<User> GetAll()
        {
            return RepositoryBase<User>.GetAll();
        }

        public static ContactData AddNewContactData(string userId, int contactDataType, string name, string value, string visibleTo)
        {
            ContactData contactData = new ContactData
            {
                UserId = userId,
                ContactDataType = contactDataType,
                Name = name,
                Value = value,
                VisibleTo = visibleTo
            };

            contactData = RepositoryBase<ContactData>.SaveOrUpdate(contactData);

            return contactData;
        }

        public static IEnumerable<ContactData> GetContactData(string userId)
        {
            IList exp = new ArrayList();

            exp.Add(NHibernate.Criterion.Expression.Eq("UserId", userId));

            IList<ContactData> list = NHibernateManager.GetObjectList<ContactData>(exp);

            return list;
        }

        public static void RemoveContactData(string userId, string id)
        {
            IList exp = new ArrayList();

            exp.Add(NHibernate.Criterion.Expression.And(
                        NHibernate.Criterion.Expression.Eq("UserId", userId),
                        NHibernate.Criterion.Expression.Eq("Id", id)));

            IList<ContactData> list = NHibernateManager.GetObjectList<ContactData>(exp);

            var contactData = list.FirstOrDefault();

            if (contactData != null)
                RepositoryBase<ContactData>.Delete(contactData);
        }
    }
}
